﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using neside3.ext;
using Newtonsoft.Json.Linq;

namespace builder_cc65
{
    public class builder_cc65 : neside3.ext.IBuilder
    {
        //插件必須有一個無參數構造函數
        public builder_cc65()
        {

        }
        public string BuilderType => "cc65";

        public async Task<bool> Build(ILogger logger, JToken buildoption)
        {
            //coll buildfiles
            List<string> buildfilters = new List<string>();
            try
            {
                var array = (buildoption as JObject)["buildfiles"] as JArray;
                foreach (var item in array)
                {
                    buildfilters.Add(item.ToString());
                }
            }
            catch (Exception err)
            {
                logger.Error("get option buildfiles error:" + err.Message);
            }
            string config = null;
            try
            {
                config = (buildoption as JObject)["config"].ToString();
            }
            catch (Exception err)
            {
                logger.Error("get option config error:" + err.Message);
            }
            List<string> libstr = new List<string>();
            try
            {
                var array = (buildoption as JObject)["libs"] as JArray;
                foreach (var item in array)
                {
                    libstr.Add(item.ToString());
                }
            }
            catch (Exception err)
            {
                logger.Error("get option libs error:" + err.Message);
            }
            var buildresult = false;
            var projectname = System.IO.Path.GetFileName(this.projectFolder);
            logger.Clear();
            Thread t = new Thread(() =>
             {
                 using (var btool = new lib.cc65.BuildTool())//可以用using机制调用end，更清晰
                 {
                     var outfolder = "temp\\" + projectname;
                     var bs = btool.Start(logger, this.projectFolder, outfolder);
                     if(!bs)
                     {
                         return;
                     }
                     List<string> buildfile = new List<string>();

                     //整理需要build的文件
                     foreach (var filter in buildfilters)
                     {
                         var files = System.IO.Directory.GetFiles(outfolder, filter);
                         foreach (var file in files)
                         {
                             buildfile.Add(file);
                         }
                     }
                     var b = btool.Build(buildfile.ToArray());
                     if (!b) return;

                     var b2 = btool.Link(projectname + ".nes", config, libstr.ToArray());
                     buildresult = b2;
                 }
             });
            t.Start();
            while (t.IsAlive)
            {
                await Task.Delay(10);
            }
            return buildresult;
        }

        UI_BuildResult _buildResultUI;
        public UIElement GetUI(ILogger logger)
        {
            if (this._buildResultUI == null)
                _buildResultUI = new UI_BuildResult();
            return _buildResultUI;
        }
        string projectFolder;
        public bool SetProject(ILogger logger, string projectfolder)
        {
            this.projectFolder = projectfolder;
            return true;
        }
    }
}
